﻿using System.Collections.Generic;
using NHibernate.Criterion;
using Ornament.MemberShip;
using Ornament.NHibernates;

namespace Ornament.Messages.Dao.NHibernateImple
{
    public class InfoReadStateDao : DaoMultiId<InfoReadState>, IInfoReadStateDao
    {
        protected override Order[] DefaultOrder
        {
            get { return new Order[] { Order.Asc("User") }; }
        }

        #region IInfoReadStateDao Members

        public InfoReadState Get(User user, Info info)
        {
            return base.Get(new object[] { user, info });
        }

        public IList<InfoReadState> GetReadState(Info info)
        {
            return CreateCriteria().Add(Restrictions.Eq("Info", info)).List<InfoReadState>();
        }

        #endregion

        protected override InfoReadState CreateObjectForLoad(object[] ids)
        {
            var user = ids[0] as User;
            var info = ids[1] as Info;
            if (user == null)
            {
                user = ids[1] as User;
                info = ids[0] as Info;
            }
            return new InfoReadState(user, info);
        }

        protected override object[] CreateIdsFromObject(InfoReadState t)
        {
            return new object[] { t.Reader, t.Info };
        }
    }
}